﻿using System;
using System.Linq;
using System.Collections.Generic;

namespace ExplorerPlus.DomainObjects
{
    public class Item
    {
        // data members
        private int intId;
        private int intCategoryId;
        private string strName;
        private string strFilePath;
        public List<ItemProperty> ItemProperties = new List<ItemProperty>();

        public Item(int categoryId) : this(categoryId, -1)
        {
        }

        public Item(int categoryId, int itemId) 
        {
            this.intCategoryId = categoryId;
            this.intId = itemId;
        }

        // properties
        public int ItemId
        {
            get
            {
                return intId;
            }
            set
            {
                intId = value;
            }
        }

        public int CategoryId
        {
            get
            {
                return intCategoryId;
            }
            set
            {
                intCategoryId = value;
            }
        }

        public string ItemName
        {
            get
            {
                return strName;
            }
            set
            {
                strName = value;
            }
        }

        public string FilePath
        {
            get
            {
                return strFilePath;
            }
            set
            {
                strFilePath = value;
            }
        }

        /// <summary>
        /// Returns an item property enumerator for the specified field id.
        /// </summary>
        public IEnumerable<Item.ItemProperty> GetItemPropertyEnumerator(int fieldId)
        {
            var query = from o in this.ItemProperties
                        where o.FieldId == fieldId
                        select o;
            return query;
        }

        /// <summary>
        /// Returns ItemPropertyValue for the specified field id.
        /// </summary>
        public string GetItemPropertyValue(int fieldId)
        {
            string value = "";

            IEnumerable<Item.ItemProperty> enumerator = GetItemPropertyEnumerator(fieldId);
            foreach (Item.ItemProperty itemProp in enumerator)
            {
                value += itemProp.ItemPropertyValue + ", ";
            }

            if (value.Length > 0)
            {
                return value.Substring(0, value.Length - 2);
            }

            return value;
        }

        /// <summary>
        /// If ItemProperty is found, then update ItemPropertyValue.
        /// Otherwise, Adds a new ItemProperty to the ItemProperties collection.
        /// </summary>
        public void SetItemPropertyValue(int fieldId, string value)
        {
            for (int i = 0; i < this.ItemProperties.Count; i++)
            {
                if (this.ItemProperties[i].FieldId == fieldId)
                {
                    this.ItemProperties[i].ItemPropertyValue = value;
                    return;
                }
            }
            AddItemProperty(fieldId, value);
        }

        /// <summary>
        /// Adds a new ItemProperty to the ItemProperties collection.
        /// </summary>
        public void AddItemProperty(int fieldId, string value)
        {
            ItemProperty newProperty = new ItemProperty();
            newProperty.FieldId = fieldId;
            newProperty.ItemPropertyValue = value;
            this.ItemProperties.Add(newProperty);
        }

        /// <summary>
        /// Removes all ItemProperty from the ItemProperties collection.
        /// </summary>
        public void RemoveItemProperty(int fieldId)
        {
            for (int i = 0; i < this.ItemProperties.Count; i++)
            {
                if (this.ItemProperties[i].FieldId == fieldId)
                {
                    this.ItemProperties.RemoveAt(i);
                    i--;
                }
            }
        }

        public class ItemProperty
        {
            public int FieldId;
            public string FieldName;
            public int ItemPropertyId;
            public string ItemPropertyValue;
        }
    }
}
